//
//  CMDBDataBaseViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/11.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let KMovieWidth : CGFloat = (CMDB_Screen_Width - KleftMargin * 4) / 3

class CMDBDataBaseViewController: CustomNormalContentViewController {
    
    var contentScrollView : UIScrollView!
    
    var filterView : CMDBConditionView             = CMDBConditionView.getInstance()
    
    var conditionResult : [String]                 = [String]()
    
    var tempOffSetY : CGFloat                      = 0
    
    lazy var summaryView : CMDBDataBaseSummaryView = {
        
        let summaryView = CMDBDataBaseSummaryView(frame: CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: 44), summaryArr: ["全部"])
        
        return summaryView
    }()
    
    private var moviesCollectionView: CMDBConditionCollectionView? = CMDBConditionCollectionView.getInstance()

    override func buildContentView() {
        super.buildContentView()
        
        view.backgroundColor              = UIColor.Hex(0xf2f2f2)
    
        contentScrollView                 = UIScrollView(frame: contentView!.bounds)
        
        contentScrollView.backgroundColor = UIColor.clearColor()
        
        contentScrollView.bounces         = false
        
        contentScrollView.delegate        = self
        
        contentView?.addSubview(contentScrollView)
        
        buildFilterView()
        
        buildMovieCollectionView()
        
        builSummaryView()
        
        loadResult()
        
    }
}


extension CMDBDataBaseViewController{
    
    func loadResult(){
        
        do {
            let getWorkParams   = CMDBHomeWorkParam()
            
            getWorkParams.token = "bc363d3a7abd40548e1c26a3162e0fb0"
            
            try CMDBWorkManager.getInstance.queryHomeWorks(getWorkParams, handler: { (homeWorksList) in
                
                    self.filterView.updataDataSource(homeWorksList)
                
                    self.filterView.frame                 = CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: CGFloat(self.filterView.workLabelCount - 1) * self.filterView.KLineHeight)
                
                    self.moviesCollectionView!.groupWorks = homeWorksList.works
                
                    let eachHeight                        = CGFloat((homeWorksList.works!.count - 1) / 3 + 1) * (35 + KMovieWidth / 3 * 4 + 25)

                    self.moviesCollectionView!.frame      = CGRect(x: 0, y: CGRectGetMaxY(self.filterView.frame) + 5, width: CMDB_Screen_Width, height: eachHeight)
                
                    let contentH                          = eachHeight + self.filterView.height
                
                    let size                              = CGSize(width: 0, height: contentH)
                
                    self.contentScrollView.contentSize    = size
                
                    self.conditionResult                  = self.filterView.conditionalArr
                
                }, error: { (rescode) in
                    
                }, failure: {
                    
            })
            
        } catch {
            
            printLog(error)
            
        }
    }

}

extension CMDBDataBaseViewController{
    
    func builSummaryView(){
        
        summaryView.alpha           = 0
        
        summaryView.mDelegate       = self
        
        summaryView.backgroundColor = UIColor.whiteColor()
        
        contentView?.addSubview(summaryView)
        
    }
    
    func buildFilterView(){
       
        buildAttributeFilterView()
        
    }
    
    func buildAttributeFilterView(){
        
       let attributeView = CMDBConditionView()
        
        attributeView.mDelegate     = self
        
        filterView = attributeView
        
        filterView.backgroundColor  = UIColor.Hex(0xfdfdfd)
        
        filterView.isAttributeLabel = true
        
        contentScrollView?.addSubview(attributeView)
        
    }
    
    func buildMovieCollectionView(){
        
        moviesCollectionView?.mDelegate     = self
        
        moviesCollectionView?.delegate      = self
        
        moviesCollectionView?.bounces       = true
        
        moviesCollectionView?.scrollEnabled = false
        
        contentScrollView?.addSubview(moviesCollectionView!)

        
    }

}


extension CMDBDataBaseViewController{
    
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        
        let itemType = ItemClickType(string: itemClickType!)
        
        switch itemType {
        
        case .DataBaseConditionShowArrowBtnClick:
            
            filterView.frame   = CGRect(x: 0, y: tempOffSetY, width: filterView.width, height: filterView.height)
            
            contentScrollView.bringSubviewToFront(filterView)
            
            summaryView.alpha  = 0
            
            summaryView.hidden = true
            
            break
            
        case .CollectionViewCellClick:
            
            let selectedMovieWork = data as! CMDBWorksBean
            
            let movieDetailVc     = CMDBMovieDetatilViewController()
            
            movieDetailVc.title   = "影片详情"
            
            movieDetailVc.hidesBottomBarWhenPushed = true
            
            let nav = navigationController as! CustomNavigationControllerViewController
            
            nav.pushViewController(movieDetailVc, animated: true, isForbiwDraw: true)
            
            break
            
        case .DataBaseConditionBtnClick:
            
            conditionResult.removeAll()
            
            conditionResult = filterView.conditionalArr
            
            summaryView.summaryArr = filterView.conditionalArr
            
            break
        default:
            printLog("itemcliiiii")
        }
        
//        let button = data as! CMDBFilterButton
//        printLog(button.col)
//        printLog(button.row)
    }
}

extension CMDBDataBaseViewController : UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        if scrollView.isKindOfClass(UIScrollView.self){
            
            tempOffSetY = scrollView.contentOffset.y
            
            if scrollView.contentOffset.y == 0 {
                summaryView.alpha  = 0
                summaryView.hidden = true
            }
            
            if scrollView.contentOffset.y > 0 {
                summaryView.hidden = false
                
                filterView.frame = CGRect(x: 0, y: 0, width: filterView.width, height: filterView.height)
              
                summaryView.alpha = 0.015 * scrollView.contentOffset.y
                
                printLog(summaryView.alpha)
                
                if scrollView.contentOffset.y > 44 {
                    summaryView.alpha = 1
                }
            }
        }
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSizeMake(KMovieWidth, 35 + KMovieWidth / 3 * 4)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(15, 10, 15, 15)
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//        mDelegate?.onItemClickType!("CollectionViewCellClick", data: groupWorks![indexPath.item], indexPath: indexPath)
        printLog("eeeee")
    }

}